﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace OttoEngineSimulation
{
    public partial class Form1 : Form
    {
        List<int> positionen = new List<int>();
        private int Waitmiliseconds = 30;
        Thread thread;
        Zylinder zylinder1;
        Zylinder zylinder2;
        Zylinder zylinder3;
        Zylinder zylinder4;

        delegate void UpdateUI();

        UpdateUI updateUI;
        public Form1()
        {
            InitializeComponent();
            this.BerechnePositionen();

            this.updateUI = new UpdateUI(this.Refresh);
            this.zylinder1 = new Zylinder(this.positionen[0], this.positionen[0], this.positionen[this.positionen.Count - 1]);
            this.zylinder1.Name = "zylinder1";
            this.zylinder2 = new Zylinder(this.positionen[this.positionen.Count - 1], this.positionen[0], this.positionen[this.positionen.Count - 1]);
            this.zylinder2.Name ="zylinder2";
            this.zylinder3 = new Zylinder(this.positionen[this.positionen.Count - 1], this.positionen[0], this.positionen[this.positionen.Count - 1]);
            this.zylinder3.Name ="zylinder3";
            this.zylinder4 = new Zylinder(this.positionen[0], this.positionen[0], this.positionen[this.positionen.Count - 1]);
            this.zylinder4.Name ="zylinder4";
            this.zylinder4.Size = new Size(this.zylinder4.Size.Width + 5, this.zylinder4.Size.Height);
            this.Controls.Add(this.zylinder1);
            this.Controls.Add(this.zylinder2);
            this.Controls.Add(this.zylinder3);
            this.Controls.Add(this.zylinder4);

            this.Controls["zylinder1"].Location = new Point(0, 30);
            this.Controls["zylinder2"].Location = new Point(150, 30);
            this.Controls["zylinder3"].Location = new Point(300, 30);
            this.Controls["zylinder4"].Location = new Point(450, 30);

            this.trackBar1.Maximum = 100;
            this.trackBar1.Minimum = 0;
            
            this.trackBar1.Value = this.trackBar1.Maximum -20;           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.thread = new Thread(new ThreadStart(this.StarteSimulation));
            this.thread.Start();
            this.button1.Enabled = false;
            this.button2.Enabled = true;
        }


        private void BerechnePositionen()
        {
            int radius = 50;
            for (double i = 0.0; i < 360; i++)
            {
                double angle = i * System.Math.PI / 25;
                double tempdaten = radius * System.Math.Cos(angle);
                int x = (int)(radius * System.Math.Cos(angle));
                int y = (int)(radius * System.Math.Sin(angle));
                double erg = (150 * 150) - (y * y);
                double erg2 = Math.Sqrt(erg);
                int erg3 = (int)erg2 + x;
                if (this.positionen.Contains(erg3)) { continue; }
                this.positionen.Add(erg3);
            }
        }

        private void StarteSimulation()
        {
            List<int> temp = this.positionen;
            while (true)
            {
                for (int i = 0; i < this.positionen.Count; i++)
                {
                    try
                    {
                        this.zylinder1.hoheee = temp[i];
                        this.zylinder2.hoheee = temp[temp.Count - i - 1];
                        this.zylinder3.hoheee = temp[temp.Count - i - 1];
                        this.zylinder4.hoheee = temp[i];
                        Thread.Sleep(this.Waitmiliseconds);
                        this.Invoke(this.updateUI, null);
                    }
                    catch (ObjectDisposedException ex)
                    {
                        Environment.Exit(1);
                    }

                }
                for (int i = temp.Count - 1; i > 0; i--)
                {
                    try
                    {
                        this.zylinder1.hoheee = temp[i];
                        this.zylinder2.hoheee = temp[temp.Count - i - 1];
                        this.zylinder3.hoheee = temp[temp.Count - i - 1];
                        this.zylinder4.hoheee = temp[i];
                        Thread.Sleep(this.Waitmiliseconds);
                        this.Invoke(this.updateUI, null);
                    }
                    catch (ObjectDisposedException ex)
                    {
                        Environment.Exit(1);
                    }
                }
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
          //  this.thread.Abort();
            this.Dispose();
            //base.Close();
            Environment.Exit(1);
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            this.Waitmiliseconds = this.trackBar1.Maximum - this.trackBar1.Value;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.thread.Suspend();
            this.button2.Enabled = false;
            this.button1.Enabled = true;
        }

        private void buttonSeitenAnsicht_Click(object sender, EventArgs e)
        {
            SeitenAnsichtForm seitenAnsicht = new SeitenAnsichtForm();
            seitenAnsicht.Show();
        }


        private void Verschiebung()
        {
            int radius = 50;
            int stangenLaenge = 150; // k
            for (double i = 0.0; i < 360; i++)
            {
                double angle = i * System.Math.PI / 25;
                double tempdaten = radius * System.Math.Cos(angle);
                int x = (int)(radius * System.Math.Cos(angle));
                int y = (int)(radius * System.Math.Sin(angle));
                
                double erg1 = (stangenLaenge * stangenLaenge) - (y * y); //(k^2-y^2)
                double erg2 = Math.Sqrt(erg1);//Wurzel aus (k^2-y^2)
                double pleustangenPosiX = erg2 + x; //x + Wurzel aus (k^2-y^2)
            }
        }
    }
}
